package com.hue.netty.day02.three;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

public class ServerHandler extends SimpleChannelInboundHandler<StudentPOJO.Student> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, StudentPOJO.Student msg) throws Exception {
        System.out.println("客户端的student: id="+msg.getId()+",name="+msg.getName());
    }

    /*
        数据读取完毕之后的方法
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        //将返回的数据发送写入缓存并且刷新，一般需要编码
        ctx.writeAndFlush(Unpooled.copiedBuffer("服务端已经接收到student...", CharsetUtil.UTF_8));
    }

    //异常处理，一般需要关闭通道
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
